function createRoute(record,location) {
  let mached = []
  if (record) {
    while (record) {
      mached.unshift(record)
      record = record.parent
    }
  }
  return {
    ...location,
    mached
  }
}
function runQueue(queue,from,to,cb) {
  function next(index) {
    if (index >= queue.length) return cb()
    let hook = queue[index]
    hook(from, to, () => next(index+1))
  }
  next(0)
}
class Base {
  constructor(router) {
    this.router = router
    this.current = createRoute(null, {
      path:"/"
    })
  }
  //所有的跳转逻辑都要再这里处理
  transitionTo(location, callback) {
    let record = this.router.match(location)
    let route = createRoute(record, { path: location })
    // this.current.path == location和this.current.path == route.path一样效果
    if (this.current.path == route.path && this.current.mached.length == route.mached.length) {
      return 
    }//避免下面逻辑执行两次
    let queue = [...this.router.beforeEachHooks]
    runQueue(queue, this.current, route, () => {
      this.current = route //更新当前current对象
      callback && callback()
      this.cb && this.cb(route)
    })
  }
  listen(cb) {
    this.cb = cb
  }
}
export default Base;